package com.maila.xiaoaoschool.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.maila.xiaoaoschool.R;

import com.maila.xiaoaoschool.entity.ShopClasses;
import com.zhy.autolayout.utils.AutoUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by mark on 2018/1/27.
 */

public class CartListAdapter extends BaseAdapter {
    List<ShopClasses.DataBean.GoodBean> mdata;
    Context context;

    public CartListAdapter(List<ShopClasses.DataBean.GoodBean> mdata, Context context) {
        this.mdata = mdata;
        this.context = context;

    }

    public List<ShopClasses.DataBean.GoodBean> getMdata() {
        return mdata;
    }

    public void setMdata(List<ShopClasses.DataBean.GoodBean> mdata) {
        this.mdata = mdata;
    }

    @Override
    public int getCount() {
        return mdata.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_cartlist, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
            AutoUtils.autoSize(convertView);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.tvGoodName.setText(mdata.get(position).getGoodsname());
//        if (mdata.get(position).getIs_cx() == 1) {
//            holder.tvGoodPrice.setText("￥"+Double.parseDouble(mdata.get(position).getCxcost())*mdata.get(position).getBuynum()+"");
//        } else {
//            holder.tvGoodPrice.setText("￥"+Double.parseDouble(mdata.get(position).getCost())*mdata.get(position).getBuynum()+"");
//        }
        if(mdata.get(position).getBuynum()>0){
            holder.btnReduce.setVisibility(View.VISIBLE);
            holder.tvNum.setVisibility(View.VISIBLE);
            holder.tvNum.setText(mdata.get(position).getBuynum()+"");
        }else {
            holder.btnReduce.setVisibility(View.GONE);
            holder.tvNum.setVisibility(View.GONE);
        }
        holder.btnReduce.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mdata.get(position).getBuynum()-1>0){
                    mdata.get(position).setBuynum(mdata.get(position).getBuynum()-1);
                }else {
                    mdata.remove(position);
                }

                notifyDataSetChanged();
            }
        });
        holder.btnPlus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mdata.get(position).setBuynum(mdata.get(position).getBuynum()+1);

                notifyDataSetChanged();
            }
        });
        return convertView;
    }

    static class ViewHolder {
        @BindView(R.id.tv_good_name)
        TextView tvGoodName;
        @BindView(R.id.tv_good_price)
        TextView tvGoodPrice;
        @BindView(R.id.tv_num)
        TextView tvNum;
        @BindView(R.id.btn_reduce)
        ImageView btnReduce;
        @BindView(R.id.btn_plus)
        ImageView btnPlus;


        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }

}
